package com.px.controller;

import com.px.mapper.ShCommodityMapper;
import com.px.pojo.ShCommodity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ProviderShCommodityController {

    @Resource
    private ShCommodityMapper shCommodityMapper;

    @PostMapping("/api/provider/selectShCommodityList")
    public List<ShCommodity> selectShCommodityList(@RequestParam String commodityName,
                                                   @RequestParam Integer dicOneId,
                                                   @RequestParam Integer dicTwoId,
                                                   @RequestParam Double minPrice,
                                                   @RequestParam Double maxPrice,
                                                   @RequestParam String ram,
                                                   @RequestParam Integer pageBegin,
                                                   @RequestParam Integer pageSize)throws Exception{
        List<ShCommodity> shCommodities = shCommodityMapper.selectShCommodityList(commodityName, dicOneId, dicTwoId, minPrice,maxPrice,ram, pageBegin, pageSize);
        if("".equals(shCommodities) && null == shCommodities){
            return null;
        }
        return shCommodities;
    }

    @PostMapping("/api/provider/selectShCommodityCount")
    public Integer selectShCommodityCount(@RequestParam String commodityName,
                                          @RequestParam Integer dicOneId,
                                          @RequestParam Integer dicTwoId,
                                          @RequestParam Double minPrice,
                                          @RequestParam Double maxPrice,
                                          @RequestParam String ram)throws Exception{
        int count = shCommodityMapper.selectShCommodityCount(commodityName, dicOneId,dicTwoId,minPrice,maxPrice,ram);
        if (count > 0) {
            return count;
        }
        return 0;
    }

    @PostMapping("/api/provider/selectShCommodity")
    public ShCommodity selectShCommodity(@RequestParam Integer commodityId)throws Exception{
        ShCommodity shCommodity = shCommodityMapper.selectShCommodity(commodityId);
        if("".equals(shCommodity) && null == shCommodity){
            return null;
        }
        return shCommodity;
    }

    @PostMapping("/api/provider/insertShCommodity")
    public Integer insertShCommodity(@RequestBody ShCommodity shCommodity)throws Exception{
        int insert = shCommodityMapper.insertShCommodity(shCommodity);
        if (insert > 0) {
            return insert;
        }
        return 0;
    }

    @PostMapping("/api/provider/updateShCommodity")
    public Integer updateShCommodity(@RequestBody ShCommodity shCommodity)throws Exception{
        int update = shCommodityMapper.updateShCommodity(shCommodity);
        if (update > 0) {
            return update;
        }
        return 0;
    }

    @PostMapping("/api/provider/deleteShCommodity")
    public Integer deleteShCommodity(@RequestParam Integer id)throws Exception{
        int delete = shCommodityMapper.deleteShCommodity(id);
        if (delete > 0) {
            return delete;
        }
        return 0;
    }
}
